<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Hizlialsat extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */

 public function __construct()
    {
        parent::__construct();
            // Your own constructor code
         $this->load->model('Hizlialsat_model');
       
        /* echo "<pre>";
         var_dump($data['sepetim']);
         echo "</pre>";*/
     
      
       
    } 
	public function index()
	{

	}
     public function a21greater_than($str)
    {
        
        

        if ($str <= $this->input->post('fiyat12'))
        {
           	$this->form_validation->set_message('a21greater_than', '2 Aralik başlama sayısı 1 Aralık bitiş sayısından küçük olamaz');
			return FALSE; 
        }
        else
        {
              
			return TRUE;
        }

    } 
     public function a22greater_than($str)
    {
        
        

        if ($str <= $this->input->post('fiyat21'))
        {
           	$this->form_validation->set_message('a22greater_than', '2. Aralik bitiş sayısı 2. Aralık başlama sayısından küçük olamaz');
			return FALSE; 
        }
        else
        {
              
			return TRUE;
        }

    }     
     public function a12greater_than($str)
    {
        
        

        if ($str <= $this->input->post('price1'))
        {
           	$this->form_validation->set_message('a12greater_than', '2. fiyat aralığı 1. den büyük olmamalı.');
			return FALSE; 
        }
        else
        {
              
			return TRUE;
        }

    }

    
    public function hizli_talepler()
    {
        
        $data['profile_yazisi'] = "talepler ";
        $data['subview'] = 'hizli_al_sat/view_hizli_talepler';
        $this->load->view('layouts/default1',$data); 
    }  
    public function all_talepler()
    {
      
        $data['talepler'] = $this->hizlialsat_model->get_all_talepler_limit($this->session->userdata('user_id'))->result();  
           
     	$config['base_url'] = base_url()."hizlialsat/all_talepler";

        $config['total_rows'] = $this->hizlialsat_model->get_all_talepler($this->session->userdata('user_id'))->num_rows();
		$config['per_page'] = 20;
	
        $config['uri_segment'] = '3';
        $config['next_link'] = 'Sonraki';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['prev_link'] = 'Önceki';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';        
        
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        

        
		$config['full_tag_open'] = ' <div class="pagination pull-right"><ul>';
		$config['full_tag_close'] = '</ul></div>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
	
		$this->pagination->initialize($config);
           
           
        $data['subview'] = 'hizli_al_sat/view_all_taleplerim';
        $this->load->view('layouts/default1',$data);  
    }       
    public function user_talepler()
    {
      
        $data['taleplerim'] = $this->hizlialsat_model->get_user_talepler($this->session->userdata('user_id'))->result();  
           
        $data['subview'] = 'hizli_al_sat/view_user_taleplerim';
        $this->load->view('layouts/default1',$data);  
    }   
    public function add_to_talep()
    {
        $product_id = $this->input->post('product_id');
        $product_detail = $this->product_model->get_product($product_id)->row(0);
        $pname = str_replace(' ', '-', $product_detail->name);
        $pname = mb_strtolower($pname,'UTF-8');

        $this->form_validation->set_rules('fiyat', 'Fiyat', 'required|xss_clean|numeric');
        $this->form_validation->set_rules('adet', 'Adet', 'required|xss_clean|numeric');
   
        
        if ($this->form_validation->run() == FALSE)
    		{
    		  
              
             // die();
                $data['product'] = $this->product_model->get_product($product_id)->row(0);
                $data['category_name'] = $this->config_model->get_category_name($data['product']->category_id)->row(0)->name;
                $data['subcategory_name'] = $this->config_model->get_subcategory_name($data['product']->subcategory_id)->row(0)->name;
        
        
                $data['subview'] = 'urunler/view_hizli_talep_et';
                $this->load->view('layouts/default1',$data); 
               
    		}
    		else
    		{
   
              
           
    		  $insert_data = array(
                'user_id' => $this->session->userdata('user_id'),
                'product_id' => $this->input->post('product_id'),
                'user_price' => $this->input->post('fiyat'),
                'stock' =>  $this->input->post('adet'),
                'mod'   => 2,
              
                'dateadd' => date("Y-m-d H:i:s"),
                'dateupd' => date("Y-m-d H:i:s")
                
              
              );

              if($this->db->insert('seller',$insert_data))
              {
                    
                        $talep_data = array(
                        'product_id' => $this->input->post('product_id'),
                        'seller_id' => $this->db->insert_id(),
                        'talep_user_id' => $this->session->userdata('user_id'),
                        'fiyat' => $this->input->post('fiyat'),
                        'adet' =>  $this->input->post('adet'),
                        'dateadd' => date("Y-m-d H:i:s"),
                        'dateupd' => date("Y-m-d H:i:s")
                      );
                      if($this->db->insert('talepler',$talep_data))
                      {
                          $this->session->set_flashdata('successmsg','Talebiniz Başarıyla eklenmiştir');
    		          redirect('hizlialsat/all_talepler');
                      }
                      else
                      {
                        $this->session->set_flashdata('errormsg','Talebiniz eklenemedi hC151');
    		          redirect('hizlialsat/all_talepler');
                      }
              }
              else
              {
                   $this->session->set_flashdata('errormsg','Talebiniz eklenemedi hC155');
    		          redirect('hizlialsat/hizli_talepler');;
              }
              // aynı üründen farklı fiyatlarla girme durumu var..
                
            } 
    }

     public function adet_hatasi($str)
    {
        
        

        if ($str >= ($this->input->post('adet')-1))
        {
           	$this->form_validation->set_message('adet_hatasi', '1. Aralik adet '.($this->input->post('adet')-1).' sayısından küçük olmalı');
			return FALSE; 
        }
        else
        {
              
			return TRUE;
        }

    }      
     public function price_hatasi($str)
    {
        
        

        if ($str > $this->input->post('price1'))
        {
           	$this->form_validation->set_message('price_hatasi', '2. Aralik fiyatı 1. Aralık fiyatından büyük olmamalı');
			return FALSE; 
        }
        else
        {
              
			return TRUE;
        }

    }    
    public function add_to_hizlialsat()
    {
      /*  echo "<pre>";
        var_dump($this->input->post());
        die();*/
        
        
        $product_id = $this->input->post('product_id');
        $product_detail = $this->product_model->get_product($product_id)->row(0);
        $pname = str_replace(' ', '-', $product_detail->name);
        $pname = mb_strtolower($pname,'UTF-8');

         $this->form_validation->set_rules('fiyat', 'Fiyat', 'required|xss_clean|numeric');
        $this->form_validation->set_rules('adet', 'Adet', 'required|xss_clean|numeric');
        $this->form_validation->set_rules('kargo','Kargo','required|xss_clean');
          $this->form_validation->set_rules('fiyat11');
                     $this->form_validation->set_rules('fiyat21');
                     $this->form_validation->set_rules('fiyat22');
        if($this->input->post('cars') == 'aralikli_fiyat')
        {
                     $this->form_validation->set_rules('fiyat11');
                     $this->form_validation->set_rules('fiyat21');
                     $this->form_validation->set_rules('fiyat22');
                     $this->form_validation->set_rules('fiyat12', '1. Aralık Adet', 'required|xss_clean|numeric|callback_adet_hatasi');
              $this->form_validation->set_rules('price1', '2. Aralik Fiyat', 'required|xss_clean|numeric');
             
        
                $this->form_validation->set_rules('price2', '2. Aralik Fiyat', 'required|xss_clean|numeric|callback_price_hatasi');
        }


        
        if ($this->form_validation->run() == FALSE)
    		{
    		  
              
             // die();
                $data['product'] = $this->product_model->get_product($product_id)->row(0);
                $data['category_name'] = $this->config_model->get_category_name($data['product']->category_id)->row(0)->name;
                $data['subcategory_name'] = $this->config_model->get_subcategory_name($data['product']->subcategory_id)->row(0)->name;
        
        
                $data['subview'] = 'urunler/view_hizli_sat';
                $this->load->view('layouts/default1',$data); 
               
    		}
    		else
    		{
    		 
    		    if($this->input->post('cars') == "normal_fiyat")
                {
                    $fiyat_aralik_id = 0;
                }
                else
                {
                         $fiyat_araligi = array(
                        'aralik11' => $this->input->post('fiyat11'),
                        'aralik12' => $this->input->post('fiyat12'),
                          'aralik21' => $this->input->post('fiyat21'),
                            'aralik22' => $this->input->post('fiyat22'),
                              'fiyat1' => $this->input->post('price1'),
                                'fiyat2' => $this->input->post('price2'),
                      );
                      $this->db->insert('fiyat_araligi',$fiyat_araligi);
                      $fiyat_aralik_id =  $this->db->insert_id();
                }
              
           
    		  $insert_data = array(
                'user_id' => $this->session->userdata('user_id'),
                'product_id' => $this->input->post('product_id'),
                'user_price' => $this->input->post('fiyat'),
                'stock' =>  $this->input->post('adet'),
                'mod'   => 1,
                'kargo' => $this->input->post('kargo'),
                'fiyat_araligi_id' => $fiyat_aralik_id,
                'dateadd' => date("Y-m-d H:i:s"),
                'dateupd' => date("Y-m-d H:i:s")
                
              
              );
              if($this->db->insert('seller',$insert_data))
              {
                      $this->session->set_flashdata('successmsg','Ürününü Başarıyla eklenmiştir');
    		          redirect('main/message');
              }
              else
              {
                  $this->session->set_flashdata('errormsg','Ürününü eklenemedi seller line 84');
    		          redirect('main/message');
              }
              // aynı üründen farklı fiyatlarla girme durumu var..
                
            } 
    }
    
    
    public function urunler($sort_by = 'model', $sort_order = 'asc', $offset = 0)
    {
        //die("burdayim");
        if($this->session->userdata('goster'))
        {
            $limit = $this->session->userdata('goster');
        }
        else
        {
            $limit = 10;
        }
       
        
        $results = $this->hizlialsat_model->get_hizlial_products($limit, $offset, $sort_by, $sort_order,0);

        $this->load->library('pagination');
		$config = array();
		$config['base_url'] = site_url("hizlialsat/urunler/$sort_by/$sort_order");
        
		$config['total_rows'] = $this->hizlialsat_model->get_hizlial_products( $limit, $offset, $sort_by, $sort_order,1)->num_rows();
        
		$config['per_page'] = $limit;
		$config['uri_segment'] = 5;
         
        $config['next_link'] = 'Sonraki';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['prev_link'] = 'Önceki';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';        
        
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['first_link'] = 'İlk';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['last_link'] = 'Son';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';  
		$config['full_tag_open'] = ' <div class="pagination pull-right"><ul>';
		$config['full_tag_close'] = '</ul></div>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        
        
		$this->pagination->initialize($config);
		
		
		$data['sort_by'] = $sort_by;
		$data['sort_order'] = $sort_order;
		$data['pagination'] = $this->pagination->create_links();
        $data['products'] = $results->result();
		$data['total'] = $this->hizlialsat_model->get_hizlial_products($limit, $offset, $sort_by, $sort_order,1)->num_rows();
        $data['subview'] = 'hizli_al_sat/view_hizlial';
        $this->load->view('layouts/default3',$data); 
        
        
    }






        
        
    
   
    public function checkout()
    {
        echo "<pre>";
        var_dump($this->input->post());
        die();
    }  

    
  
         
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */